# -*- coding: utf-8 -*-
'''
Created on 30/ott/2009

@author: sax
'''

from django.core.management.color import no_style
from django.contrib import admin
from django.db import models, transaction, connection
from django.test import TransactionTestCase
import unittest

class TestMasterModel(models.Model):
    name = models.CharField(max_length=30, blank=True, null=True)
    char_field = models.CharField(max_length=30, blank=True, null=True)
    date_field = models.DateField(blank=True, null=True)
    email_field = models.EmailField(blank=True, null=True)
    class Meta:
        verbose_name="TEST_MASTER_MODEL"
        app_label = "xadmin"
        
class TestDetailModel(models.Model):
    name = models.CharField(max_length=30, blank=True, null=True)
    fk = models.ForeignKey(TestMasterModel, blank=True, null=True)
    
    class Meta:
        verbose_name="TEST_DETAIL_MODEL"
        app_label = "xadmin"
        

class ModelProvider(object):
    TABLES = [TestMasterModel, TestDetailModel]  
    
    def _create_tables(self):
        transaction.enter_transaction_management()
        transaction.managed(True)
        connection.close()
        
        tables = connection.introspection.table_names()        
        for table in self.TABLES:
            if not connection.introspection.table_name_converter(table._meta.db_table) in tables:
                cursor = connection.cursor()
                sql, references = connection.creation.sql_create_model(table, no_style(), set() )
                for statement in sql:
                    cursor.execute(statement)            
    
    def _destroy_tables(self):
        tables = connection.introspection.table_names()      
        for table in self.TABLES[::-1]:  
            if connection.introspection.table_name_converter(table._meta.db_table) in tables:
                cursor = connection.cursor()
                sql = connection.creation.sql_destroy_model(table, [] , no_style())
                for statement in sql:
                    cursor.execute(statement)            
        transaction.rollback()
        transaction.leave_transaction_management()
                
class ModelProviderTest(unittest.TestCase, ModelProvider):
    def setUp(self):
        super(ModelProviderTest, self).setUp()
        self._create_tables()
        
    def tearDown(self):
        super(ModelProviderTest, self).tearDown()
        self._destroy_tables()




from django.contrib.admin.sites import AdminSite
from xadmin.admin import XModelAdmin

class AdminModelManager(object):
    ADMIN_CLASS = XModelAdmin
    admin_site = None
    
    def _init_admin_model_manager(self):
        self.admin_site = AdminSite("aa", "aa")
        #self.admin_site.register(TestMasterModel, self.ADMIN_CLASS)
        
    def set_urls(self):
        from django.conf.urls.defaults import patterns, include, handler500 as h500, handler404 as h404
        from django.conf import settings

        class urlconf(object):
            urlpatterns=None
            handler500 = h500
            handler404 = h404
            
        u = urlconf()
        u.urlpatterns = patterns('', (r'^xadmin/', include(self.admin_site.urls)) , )
        settings.ROOT_URLCONF = u

    def _register(self, model, model_admin):
        try:
            self.admin_site.unregister(model)
        except admin.sites.NotRegistered:
            pass
        
        self.admin_site.register(model, model_admin)
        self.set_urls() 
